<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>编辑收货地址</title>
    <link rel="stylesheet" href="css/amazeui.min.css" />
    <link rel="stylesheet" href="css/app.css" />
    <link rel="stylesheet" href="css/style.css" />
    <style>
        .diqu select {
            font-size: 14px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
    </style>
</head>

<body>
    <header class="am-u">
        <div class="am-u-sm-12 head_nav" style="margin:0;">
            <div class="am-u-sm-2 am-vertical-align">
                <a href="my_modify.html">
                    <img src="img/return@2x.png" class="am-vertical-align-middle" alt="">
                </a>
            </div>
            <div class="am-u-sm-8 head_text">
                编辑收货地址
            </div>
            <div class="am-u-sm-2">
            </div>
        </div>
    </header>
    <div style="clear:both"></div>
    <div class="am-u myDeviceLists">
        <div class="myadd">
            <input type="text" class="myadinput" value="加载中" placeholder="请输入收货人姓名" id="receiver_name" />
            <input type="text" class="myadinput" value="加载中" placeholder="请输入收货人号码" id="receiver_phone" />
            <a href="javascript:;" class="myadlink">
                <div class="myadle diqu">
                    <select id="cmbProvince" name="cmbProvince"></select>
                    <select id="cmbCity" name="cmbCity"></select>
                    <select id="cmbArea" name="cmbArea"></select>
                </div>
                <!-- <img src="img/right1.png" alt="" class="myadpic" /> -->
            </a>
            <input type="text" class="myadinput" value="加载中" placeholder="请输入详细地址" id="address" />
        </div>
        <a href="javascript:;" class="myad2link" data-status="0">
            <span class="myad2s"></span>设为默认地址
        </a>
        <button class="bulink" style="width:85%">修改</button>
    </div>
    <div style="clear: both;"></div>

    <script src="js/jquery.min.js" type="text/javascript"></script>
    <script src="js/searchCity.js"></script>\
    <script type="text/javascript">
        addressInit('cmbProvince', 'cmbCity', 'cmbArea');
    </script>

</body>
<script type="text/javascript">
    $(function () {
        // 默认选择
        $('.myad2link').click(function () {
            if ($(this).attr("data-status") == "0") {
                $(this).addClass("on");
                $(this).attr("data-status", "1")
            } else {
                $(this).removeClass("on");
                $(this).attr("data-status", "0")
            }
        })

        // 数据写入
        var receiver_name = sessionStorage.getItem("name"); //姓名
        var receiver_phone = sessionStorage.getItem("phone"); //手机号
        var province = sessionStorage.getItem("province"); //省
        var city = sessionStorage.getItem("city"); //市
        var district = sessionStorage.getItem("district"); //区
        var address = sessionStorage.getItem("address"); //详情
        var status = sessionStorage.getItem("status"); //是否默认
        $('#receiver_name').val(receiver_name);
        $('#receiver_phone').val(receiver_phone);
        $('#cmbProvince').find("option:selected").text(province);
        $('#cmbCity').find("option:selected").text(city);
        $('#cmbArea').find("option:selected").text(district);
        $('#address').val(address);
        console.log(status)
        if (status == 1) {
            $('.myad2link').addClass('on');
            $('.myad2link').attr("data-status", "1")
        } else {
            $('.myad2link').removeClass('on');
            $('.myad2link').attr("data-status", "0")
        }

        // 手机号判断
        var phone2 = true;
        $('#receiver_phone').blur(function () {
            var phone = document.getElementById('receiver_phone').value;
            if (!(/^1[34578]\d{9}$/.test(phone))) {
                alert("手机号码有误，请重填");
                return phone2 = false;
            } else {
                return phone2 = true;
            }
        })

        $('.bulink').on("click", function () {
            var token1 = sessionStorage['token'];;
            var method = "Shop|shop_user_address_edit";
            var address_id = sessionStorage.getItem("address_id");
            var name1 = $('#receiver_name').val();
            var receiver_phone1 = $('#receiver_phone').val();
            var province1 = $('#cmbProvince').find("option:selected").text();
            var city1 = $('#cmbCity').find("option:selected").text();
            var district1 = $('#cmbArea').find("option:selected").text();
            var address1 = $('#address').val();
            var status1 = $('.myad2link').attr("data-status");
            // console.log("address_id:" + address_id + " address1:" + address1 + " receiver_phone1:" + receiver_phone1 + " province1:" + province1 + " city1:" + city1 + " district1:" + district1 + " status1:" + status1);
            // console.log(phone2)
            if (phone2 == true) {
                $.ajax({
                    type: 'POST',
                    url: "http://www.rice.com/app/api/h5_access",
                    contentType: "application/x-www-form-urlencoded; charset=utf-8",
                    datatype: "JSON",
                    data: {
                        token: token1,
                        method: method,
                        address_id: address_id,
                        receiver_name: name1, //姓名
                        receiver_phone: receiver_phone1, //手机号
                        province_name: province1,
                        city_name: city1,
                        county_name: district1,
                        address: address1,
                        status: status1
                    },
                    success: function (data) {
                        var data1 = JSON.parse(data)
                        if (data1.status == 1) {
                            alert(data1.message);
                            window.location.href = "my_modify.html";
                        } else {
                            alert(data1.message)
                        }
                    }
                })
            } else {
                alert("手机号码有误，请重填");
            }

        })
    })
</script>

</html>